<template>
  <div class="add-form">
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="dataForm" :model="formBase" label-position="left" label-width="150px">
        <el-form-item label="标题：" prop="title">
          <el-input v-model="formBase.title"></el-input>
        </el-form-item>
        <el-form-item label="封面图片：" prop="cover">
          <el-input v-model="formBase.cover"></el-input>
        </el-form-item>
        <el-form-item label="分类id：" prop="blogCategoryId">
          <el-input v-model="formBase.blogCategoryId"></el-input>
        </el-form-item>
        <el-form-item label="排序：" prop="index">
          <el-input v-model="formBase.index"></el-input>
        </el-form-item>
        <el-form-item label="链接：" prop="link">
          <el-input v-model="formBase.link"></el-input>
        </el-form-item>
        <el-form-item label="内容主体：" prop="content">
          <el-input v-model="formBase.content"></el-input>
        </el-form-item>
        <el-form-item label="状态：1为未发布，2为已发布：" prop="status">
          <el-input v-model="formBase.status"></el-input>
        </el-form-item>
        <el-form-item label="发布人：" prop="creator">
          <el-input v-model="formBase.creator"></el-input>
        </el-form-item>
        <el-form-item label="创建时间：" prop="createTime">
          <el-input v-model="formBase.createTime"></el-input>
        </el-form-item>
        <el-form-item label="更新时间：" prop="updateTime">
          <el-input v-model="formBase.updateTime"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">取消</el-button>
        <el-button type="primary" @click="createData">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { updateData, addData } from '@/api/blog'
export default {
  name: 'add',
  props: ['dialogTitle'],
  data() {
    return {
      dialogFormVisible: false,
      formBase: {
        number: '',
        name: ''
      }
    }
  },
  computed: {},
  methods: {
    // 弹层显示
    dialogFormV(val) {
      this.dialogFormVisible = true
      if (val !== undefined) {
        this.formBase = val
      }
    },
    // 弹层隐藏
    dialogFormH() {
      this.dialogFormVisible = false
    },

    // 表单提交
    createData() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          this.dialogFormH()
          let data = {
            ...this.formBase
          }
          if (this.formBase.id) {
            await updateData(data).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            await addData(this.formBase).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    }
  },
  // 挂载结束

  mounted: function() {},
  // 创建完毕状态
  created() {
  },
  // 组件更新
  updated: function() {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
